#include "EngineEntityCollection.h"

EngineEntityCollection::EngineEntityCollection(void)
{
}

EngineEntityCollection::~EngineEntityCollection(void)
{
}

void EngineEntityCollection::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	context->StartItem(name, "EngineEntityCollection", "1.0");
	context->StartItem("Entities");
	for (list<EnginePointer<EngineEntity>>::iterator it = this->begin(); it != this->end(); it++)
	{
		(*it)->Serialize(context, "Entity");
	}
	context->EndItem("Entities");
	context->EndItem(name);
}

EngineEntityCollection* EngineEntityCollection::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	EngineEntityCollection* entityCollection = new EngineEntityCollection();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "Entities") == 0)
		{
			int componentIndex = 0;
			context->MoveToChild(index);
			childName = context->ChildName(componentIndex);

			while (childName != NULL)
			{
				if (strcmp(childName, "Entity") == 0)
				{
					context->MoveToChild(componentIndex);
					entityCollection->push_back(EngineEntity::Deserialize(context));
					context->MoveToParent();
				}
				childName = context->ChildName(++componentIndex);
			}
			context->MoveToParent();
		}
		childName = context->ChildName(++index);
	}
	
	return entityCollection;
}